package com.wxg.domain.table;

import java.util.ArrayList;
import java.util.List;

public class TableDomain {

	/**
	 * 表头
	 */
	private List<String> columnHead;
	
	/**
	 * Table datas
	 */
//	private List<List<RowDomain>> tableDatas;
	private List<List<Object>> tableDatas;
	
	/**
	 * 上一次获取数据时的副本。
	 */
	private Object[][] cachedatas;
	
	private String[] cacheheads;
	
	public TableDomain(){
		columnHead = new ArrayList<String>();
//		tableDatas = new ArrayList<List<RowDomain>>();
		tableDatas = new ArrayList<List<Object>>();
	}
	
	/**
	 * 获取表头（标题）
	 * @return
	 */
	public String[] head(){
		if(columnHead.size() == 0){
			throw new TableDomain.TableNameException("column head is empty.");
		}
		if(cacheheads == null || cacheheads.length != columnHead.size()){
			cacheheads = columnHead.toArray(new String[]{});
		}
		return cacheheads;
	}
	
	/**
	 * 设置表头
	 * @param args
	 */
	public void head(String ...args){
		if(null == args){
			throw new TableDomain.TableNameException("h(String ...args) args is null.");
		}
		for(String arg : args){
			columnHead.add(arg);
		}
	}
	
	/**
	 * 获取表数据
	 * @return
	 */
	public Object[][] data(){
		int outsize = tableDatas.size();
		int insize = columnHead.size();
		if(insize == 0){
			throw new TableDomain.TableNameException("column head is empty.");
		}
		if(outsize == 0){
			//throw new TableDomain.TableNameException("table datas is empty.");
			return new Object[][]{};
		}
		/**
		 * 缓存处理， 提升性能！
		 */
		if(null == cachedatas || cachedatas.length != outsize){
			cachedatas = new Object[outsize][insize];
			for(int i=0; i<outsize; i++){
				for(int j=0; j<insize; j++){
					cachedatas[i][j] = tableDatas.get(i).get(j);
				}
			}
		}
		return cachedatas;
	}
	
	/**
	 * 设置表数据
	 * @param args
	 */
	public void data(Object ...args){
		if(null == args){
			throw new TableDomain.TableNameException("args is null.");
		}
		int insize = columnHead.size();
		List<Object> list = new ArrayList<Object>(insize);
		for(int i=0; i<insize; i++){
			list.add(args[i]);
		}
		tableDatas.add(list);
	}
	
	/**
	 * 删除一行表数据
	 * @param rowindex 行索引
	 */
	public void remove(int rowindex){
		int size = tableDatas.size();
		if(rowindex < size){
			tableDatas.remove(rowindex);
		}
	}
	
	/**
	 * 批量删除行数据
	 * @param args 行索引的数组
	 */
//	public void removes(int ...args){
//		List<Object> waitForRemoves = new ArrayList<Object>();
//		int size = tableDatas.size();
//		if(null != args){
//			for(int a : args){
//				if(a < size){
//					waitForRemoves.add( tableDatas.get(a) );
//				}
//			}
//			tableDatas.removeAll(waitForRemoves);
//		}
//	}
	
	/**
	 * 这里好尴尬啊！
	 * List<Integer> list
	 * list.toArray(new Integer[]{})
	 * 
	 * 不能转化成 int[]
	 * 
	 * 批量删除行数据
	 * @param args 行索引的数组
	 */
	public void removes(Integer ...args){
		List<Object> waitForRemoves = new ArrayList<Object>();
		int size = tableDatas.size();
		if(null != args){
			for(int a : args){
				if(a < size){
					waitForRemoves.add( tableDatas.get(a) );
				}
			}
			tableDatas.removeAll(waitForRemoves);
		}
	}
	
	/**
	 * 自定义的运行时异常
	 * @author wxg
	 *
	 */
	public class TableNameException extends RuntimeException{
		private static final long serialVersionUID = 6131193668824300709L;

		public TableNameException() {
			super();
		}

		public TableNameException(String message, Throwable cause, boolean enableSuppression,
				boolean writableStackTrace) {
			super(message, cause, enableSuppression, writableStackTrace);
		}

		public TableNameException(String message, Throwable cause) {
			super(message, cause);
		}

		public TableNameException(String message) {
			super(message);
		}

		public TableNameException(Throwable cause) {
			super(cause);
		}
		
	}
	
	
	
}
